package config

import (
	"fmt"
	"github.com/spf13/viper"
)

type Config struct {
	Server   ServerConfig
	Database DatabaseConfig
	Redis    RedisConfig
	RabbitMQ RabbitMQConfig
}

type RabbitMQConfig struct {
	Host     string
	Port     int
	User     string
	Password string
}

type ServerConfig struct {
	Name string
	Host string
	Port int
	Mode string
}

type DatabaseConfig struct {
	Driver       string
	Host         string
	Port         int
	Username     string
	Password     string
	Dbname       string
	MaxIdleConns int
	MaxOpenConns int
}

type RedisConfig struct {
	Host string
	Port int
	DB   int
}

var AppConfig *Config

func LoadConfig(configPath string) error {
	viper.SetConfigFile(configPath)
	viper.SetConfigType("yaml")
	if err := viper.ReadInConfig(); err != nil {
		return fmt.Errorf("failed to read config file: %w", err)
	}

	AppConfig = &Config{}

	if err := viper.Unmarshal(AppConfig); err != nil {
		return fmt.Errorf("failed to unmarshal config: %w", err)
	}

	return nil
}
